<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function sendAjax(){
            // 1.创建XMLHttpRequest对象
            let xhr = new XMLHttpRequest()

            // 2.监听状态的变化
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4 && xhr.status === 200){
                    let res = xhr.responseText
                    // console.log(res)

                    let obj = JSON.parse(res);
                    if (obj.status === 1){
                        var user = obj.data
                        document.getElementById('result').innerText = user.name
                    }else{
                        document.getElementById('result').innerText = obj.msg
                    }

                }
            }

            // 3.与服务器建立连接
            xhr.open('get', 'welcome?username=alice')

            // 4.发送请求
            xhr.send()
        }
    </script>
</head>
<body>
    <button onclick="sendAjax()">发送Ajax请求</button>
    <hr>
    <div id="result" style="width: 300px; height: 300px; border: 1px solid #333;">

    </div>
</body>
</html>